package com.prolestar.report.service.impl;

import com.prolestar.Enum.ReportStatus;
import com.prolestar.common.core.domain.AjaxResult;
import com.prolestar.common.core.domain.entity.SysDept;
import com.prolestar.common.core.domain.entity.SysUser;
import com.prolestar.common.core.domain.model.LoginUser;
import com.prolestar.common.utils.DateUtils;
import com.prolestar.common.utils.ServletUtils;
import com.prolestar.report.domain.PsPublicsentimentReport;
import com.prolestar.report.domain.PsPublicsentimentReportVO;
import com.prolestar.report.mapper.PsPublicsentimentReportMapper;
import com.prolestar.report.service.IPsPublicsentimentReportService;
import com.prolestar.service.TokenServices;
import com.prolestar.system.service.ISysDeptService;
import com.prolestar.system.service.ISysRoleService;
import com.prolestar.system.service.ISysUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 舆情上报Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-11
 */
@Service
public class PsPublicsentimentReportServiceImpl implements IPsPublicsentimentReportService {
    @Autowired
    private PsPublicsentimentReportMapper psPublicsentimentReportMapper;

    @Autowired
    private TokenServices tokenServices;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysRoleService sysRoleService;



    /**
     * 查询舆情上报
     *
     * @param id 舆情上报主键
     * @return 舆情上报
     */
    @Override
    public PsPublicsentimentReport selectPsPublicsentimentReportById(Long id) {
        return psPublicsentimentReportMapper.selectPsPublicsentimentReportById(id);
    }

    /**
     * 查询舆情上报列表
     *
     * @param psPublicsentimentReport 舆情上报
     * @return 舆情上报
     */
    @Override
    public List<PsPublicsentimentReportVO> selectPsPublicsentimentReportList(PsPublicsentimentReport psPublicsentimentReport) {



        LoginUser loginUser = tokenServices.getLoginUser(ServletUtils.getRequest());
        Long deptId = loginUser.getDeptId();
        Long userId = loginUser.getUserId();

        Set<String> permission = sysRoleService.selectRolePermissionByUserId(userId);

        psPublicsentimentReport.setReportUnit(deptId);

        List<PsPublicsentimentReport> reportList = psPublicsentimentReportMapper.selectPsPublicsentimentReportList(psPublicsentimentReport);

        List<PsPublicsentimentReport> reportList1 = psPublicsentimentReportMapper.selectPsPublicsentimentReportUserList(psPublicsentimentReport);

        ArrayList<PsPublicsentimentReportVO> reportVOS = new ArrayList<>();
        ArrayList<PsPublicsentimentReportVO> reportVOS1 = new ArrayList<>();
        reportList.forEach(rep -> {
            PsPublicsentimentReportVO reportVO = new PsPublicsentimentReportVO();
            SysUser sysUser = sysUserService.selectUserById(rep.getReportBy());
            SysDept sysDept = sysDeptService.selectDeptById(rep.getReportUnit());
            reportVO.setReportBy(sysUser.getNickName());
            reportVO.setReportUnit(sysDept.getDeptName());
            reportVO.setId(rep.getId());
            reportVO.setReportTime(rep.getReportTime());
            reportVO.setReportContent(rep.getReportContent());
            reportVO.setLinkUrl(rep.getLinkUrl());
            reportVO.setMediaType(rep.getMediaType());
            reportVO.setPstitle(rep.getPstitle());
            reportVO.setTendentiousness(rep.getTendentiousness());
            reportVO.setReleaseTime(rep.getReleaseTime());
            reportVO.setSrcWebset(rep.getSrcWebset());
            reportVO.setStatus(rep.getStatus());
            reportVOS.add(reportVO);
        });
        reportList1.forEach(rep1 -> {
            PsPublicsentimentReportVO reportVO = new PsPublicsentimentReportVO();
            SysUser sysUser = sysUserService.selectUserById(rep1.getReportBy());
            SysDept sysDept = sysDeptService.selectDeptById(rep1.getReportUnit());
            reportVO.setReportBy(sysUser.getNickName());
            reportVO.setReportUnit(sysDept.getDeptName());
            reportVO.setId(rep1.getId());
            reportVO.setReportTime(rep1.getReportTime());
            reportVO.setReportContent(rep1.getReportContent());
            reportVO.setLinkUrl(rep1.getLinkUrl());
            reportVO.setMediaType(rep1.getMediaType());
            reportVO.setPstitle(rep1.getPstitle());
            reportVO.setTendentiousness(rep1.getTendentiousness());
            reportVO.setReleaseTime(rep1.getReleaseTime());
            reportVO.setSrcWebset(rep1.getSrcWebset());
            reportVO.setStatus(rep1.getStatus());
            reportVOS1.add(reportVO);
        });
        if (permission.contains("admin")||permission.contains("conservator")){
            return reportVOS;
        }else {
            return reportVOS1;
        }
    }

    /**
     * 新增舆情上报
     *
     * @param psPublicsentimentReport 舆情上报
     * @return 结果
     */
    @Override
    public int insertPsPublicsentimentReport(PsPublicsentimentReport psPublicsentimentReport) {
        /*当前系统操作人和操作单位*/
        LoginUser loginUser = tokenServices.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUserId();
        /*管理员新建就是已上报*/
        Set<String> permission = sysRoleService.selectRolePermissionByUserId(userId);
        if (permission.contains("admin")||permission.contains("conservator")){
            psPublicsentimentReport.setStatus(ReportStatus.HADREPORT);
        } else if (psPublicsentimentReport.getStatus() == ReportStatus.DRAFT){
            psPublicsentimentReport.setStatus(ReportStatus.HADREPORT);
        }
        else {
            psPublicsentimentReport.setStatus(ReportStatus.DRAFT);
        }
        psPublicsentimentReport.setReleaseTime(DateUtils.getNowDate());
        Long deptId = loginUser.getDeptId();
        psPublicsentimentReport.setReportBy(userId);
        psPublicsentimentReport.setReportUnit(deptId);
        return psPublicsentimentReportMapper.insertPsPublicsentimentReport(psPublicsentimentReport);
    }

    /**
     * 修改舆情上报
     *
     * @param psPublicsentimentReport 舆情上报
     * @return 结果
     */
    @Override
    public int updatePsPublicsentimentReport(PsPublicsentimentReport psPublicsentimentReport) {
        return psPublicsentimentReportMapper.updatePsPublicsentimentReport(psPublicsentimentReport);
    }

    /**
     * 批量删除舆情上报
     *
     * @param ids 需要删除的舆情上报主键
     * @return 结果
     */
    @Override
    public int deletePsPublicsentimentReportByIds(Long[] ids) {
        return psPublicsentimentReportMapper.deletePsPublicsentimentReportByIds(ids);
    }

    /**
     * 删除舆情上报信息
     *
     * @param id 舆情上报主键
     * @return 结果
     */
    @Override
    public int deletePsPublicsentimentReportById(Long id) {
        return psPublicsentimentReportMapper.deletePsPublicsentimentReportById(id);
    }

    @Override
    public List<PsPublicsentimentReport> selectPsPublicsentimentReportLists(PsPublicsentimentReport psPublicsentimentReport) {
        return psPublicsentimentReportMapper.selectPsPublicsentimentReportList(psPublicsentimentReport);
    }

    /*
    * 舆情上报上报操作 --普通人员才可上报
    * */
    @Override
    public AjaxResult appear(PsPublicsentimentReport psPublicsentimentReport) {
        Long status = psPublicsentimentReport.getStatus();
        if (status == null){
            return AjaxResult.error("状态不能为空");
        }
        if (status == ReportStatus.DRAFT){ // 草稿状态改为已上报状态
            psPublicsentimentReport.setStatus(ReportStatus.HADREPORT);
            psPublicsentimentReportMapper.updatePsPublicsentimentReport(psPublicsentimentReport);
        }
        return AjaxResult.success("上报成功");
    }

    /*
    * 舆情上报处置操作 ——管理员才可处理
    * */
    @Override
    public AjaxResult dispose(PsPublicsentimentReport psPublicsentimentReport) {
        Long status = psPublicsentimentReport.getStatus();
        if (status == null){
            return AjaxResult.error("状态不能为空");
        }
        if (status == ReportStatus.HADREPORT){ // 上报状态改为已处理状态
            psPublicsentimentReport.setStatus(ReportStatus.PROCESSED);
            psPublicsentimentReportMapper.updatePsPublicsentimentReport(psPublicsentimentReport);
        }
        return AjaxResult.success("处理成功");
    }

    /**
     * 舆情上报 撤回操做
     * @param psPublicsentimentReport
     * @return
     */
    @Override
    public AjaxResult withdraw(PsPublicsentimentReport psPublicsentimentReport) {
        Long status = psPublicsentimentReport.getStatus();
        if (status == null){
            return AjaxResult.error("状态不能为空");
        }
        if (status == ReportStatus.HADREPORT){
            psPublicsentimentReport.setStatus(ReportStatus.RETURNED);
            psPublicsentimentReportMapper.updatePsPublicsentimentReport(psPublicsentimentReport);
        }
        return AjaxResult.success("撤回成功");
    }


}
